package com.yztstudy.Controller;

import com.yztstudy.Entity.*;
import com.yztstudy.Service.*;
import com.yztstudy.Utils.CartsTotalPriceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Order的表示层
 */

@Controller
@Scope("prototype") //spring注解的bean可以变成多例
public class OrderController {

    @Autowired
    private DeskService deskService;

    @Autowired
    private CartService cartService;

    @Autowired
    private FoodTypeService foodTypeService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ItemService itemService;


    /**
     * 用户下单
     */
    @GetMapping("/generateOrder/{deskID}/{typeID}")
    public String generateOrder(@PathVariable("deskID") Integer deskID, @PathVariable("typeID") Integer typeID, ModelMap modelMap)
    {
        //1.取数据
        Desk desk = deskService.getDeskByID(deskID); //取出当前的餐桌
        List<Cart> carts = cartService.getCarts(deskID); //取出当前餐桌的所有订单
        List<FoodType> foodTypes = foodTypeService.getAllFoodType(); //获取所有的可用菜系
        double totalPrice = CartsTotalPriceUtils.getTotalPrice(carts); //总价格

        //2.添加订单
        int count = orderService.addOrder(deskID);
        if (count == carts.size() + 2)
        {
            //成功就跳转道结账页面
            //绑定数据
            modelMap.addAttribute("carts", carts);
            modelMap.addAttribute("desk", desk);
            modelMap.addAttribute("totalPrice", totalPrice);
            return "clientOrderList";
        }else {
            //失败就返回原来的界面
            //绑定数据
            modelMap.addAttribute("carts", carts);
            modelMap.addAttribute("foodTypes", foodTypes);
            modelMap.addAttribute("desk", desk);
            modelMap.addAttribute("type_id", typeID);
            modelMap.addAttribute("totalPrice", totalPrice);
            modelMap.addAttribute("flag", false);
            return "clientCart";
        }
    }

    /**
     * 后台查看订单
     */
    @GetMapping("/OrderQueryList")
    public String findOrders(ModelMap modelMap)
    {
        //1.获取数据
        List<Order> orders = orderService.getAllOrders();

        //2.绑定数据
        modelMap.addAttribute("orders", orders);

        return "orderList";
    }

    /**
     * 查看订单下面的所有菜品
     */
    @GetMapping("/orderDetail/{oid}")
    public String orderDetail(@PathVariable("oid") Integer oid, ModelMap modelMap)
    {
        //1.查询
        List<Item> items = itemService.getItemsByOrderID(oid);

        //2.绑定数据
        modelMap.addAttribute("items", items);

        return "orderDetail";
    }

    /**
     * 客户端通知服务员结账
     */
    @GetMapping("/infoWaiter/{deskID}")
    public String infoWaiter(@PathVariable("deskID") Integer deskID, ModelMap modelMap)
    {
        //1.创建Desk对象
        Desk desk = new Desk();
        desk.setId(deskID);
        desk.setState(0);

        //2.取消预定
        deskService.cancelState(desk);

        //3.绑定可用的餐桌
        List<Desk> desks = deskService.getFreeDesks();
        modelMap.addAttribute("desks", desks);

        //4.跳转到初始的点餐页面
        return "customerMain";
    }

    @GetMapping("pay/{deskID}/{Oid}")
    public void pay(@PathVariable("deskID") Integer deskID, @PathVariable("Oid") Integer Oid, HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html;charset=utf-8");

        //1.结账
        orderService.pay(deskID, Oid);

        //2.回到查询所有订单的界面
        response.getWriter().write("<script>alert('结账成功!'); location.href = '/OrderQueryList/';</script>");
    }

}
